<?php

class apidesigner_IndexController extends Zend_Controller_Action
{
  function init() {
	$this->view->headLink()->appendStylesheet($this->view->baseUrl().'/ressources/apidesigner/style.css');
  }

  function indexAction() {

    $engine = Myzen_Oauth_Query_Singleton::getInstance();    
    $data = $engine->query('apidefinition','module','list');
//print_r($data);
//exit;
	$this->view->listModules = unserialize($data);
	
	$this->view->Breadcrumbs(array(0 => array('text' => 'API Designer' )));
	$this->saveSidebarLinks();
    
  } //end IndexAction

  function createnewmoduleAction() {
  	$name = $this->_getParam('moduleName');
  	$description = $this->_getParam('moduleDefinition');
  	if(empty($name) || empty($description)) {
  		$this->_forward('index');
  		return;	
  	}
    $engine = Myzen_Oauth_Query_Singleton::getInstance();    
    $data = $engine->query('apidefinition','module','create',array('name' => $name, 'description' => $description));

    
    $creationOk = unserialize($data);
    
    if('KO' == $creationOk['status']) {
       $this->view->messageDisplay()->setMessage($creationOk['error']['message'],'error');
    } else {
       $this->view->messageDisplay()->setMessage('Module has succesfully been created','confirmation');	
	}
    $this->_forward('index');
  }

  function moduleAction() {
  	$name = $this->_getParam('moduleName');
  	if(empty($name)) {
  		$this->_forward('index');
  		return;	
  	}
    $engine = Myzen_Oauth_Query_Singleton::getInstance();    
    $data = $engine->query('apidefinition','module','get',array('name' => $name));

    $moduleData = unserialize($data);
    
    if('KO' == $moduleData['status']) {
   	  $this->_forward('index');
  	  return;	   	
    }
    
    $this->view->module = $moduleData['module'];
	$this->view->moduleData = $moduleData;
	$this->view->moduleName = $moduleData['module']['name'];
	
	
	$this->view->Breadcrumbs(array(
		0 => array(
			'text' => 'API Designer', 
			'url'  => '/apidesigner/'
			)
		,
		1 => array(
			'text' => $moduleData['module']['name']
			)
		)		
	);
	$this->saveSidebarLinks();
		
  } //end moduleAction

  function createnewcontrollerAction() {
  	$name = $this->_getParam('controllerName');
  	$moduleName = $this->_getParam('moduleName');
  	$description = $this->_getParam('controllerDescription');
  	if(empty($name) || empty($description) || empty($moduleName)) {
  		$this->_forward('module');
  		return;	
  	}
    $engine = Myzen_Oauth_Query_Singleton::getInstance();    
    $data = $engine->query('apidefinition','controller','create',array('name' => $name,'moduleName' => $moduleName, 'description' => $description));
    $creationOk = unserialize($data);
	
	if('KO' == $creationOk['status']) {
       $this->view->messageDisplay()->setMessage($creationOk['error']['message'],'error');
    } else {
       $this->view->messageDisplay()->setMessage('Controller has succesfully been created','confirmation');	
	}
	
    $this->_forward('module');
  } //end createnewcontroller

  function controllerAction() {
  	$controllerName = $this->_getParam('controllerName');
  	$moduleName = $this->_getParam('moduleName');
	
  	if(empty($controllerName) || empty($moduleName)) {
  		$this->_forward('module');
  		return;	
  	}
  	
    $engine = Myzen_Oauth_Query_Singleton::getInstance();    
    $data = $engine->query('apidefinition','controller','get',array('name' => $controllerName,'moduleName' => $moduleName));

    $controllerData = unserialize($data);

    if('KO' == $controllerData['status']) {
   	  $this->_forward('module');
  	  return;	   	
    }

    $this->view->moduleName = $controllerData['controller']['moduleName'];
    $this->view->controllerName = $controllerData['controller']['name'];
    $this->view->controllerData = $controllerData;
    

	$this->view->Breadcrumbs(array(
		0 => array(
			'text' => 'API Designer', 
			'url'  => '/apidesigner/'
			)
		,
		1 => array(
			'text' => $controllerData['controller']['moduleName'], 
			'url'  => '/apidesigner/'.$controllerData['controller']['moduleName'].'/'
			)
		,
		2 => array(
			'text' => $controllerData['controller']['name']
			)
		)		
	);
	$this->saveSidebarLinks();

    
  } //end controllerAction

  function createnewactionAction() {
  	$name = $this->_getParam('actionName');
  	$controllerName = $this->_getParam('controllerName');
  	$moduleName = $this->_getParam('moduleName');
  	$description = $this->_getParam('actionDescription');

  	if(empty($name) || empty($description) || empty($controllerName) || empty($moduleName)) {
  		$this->_forward('index');
  		return;	
  	}
    $engine = Myzen_Oauth_Query_Singleton::getInstance();    
    $data = $engine->query('apidefinition','action','create',array('name' => $name,'controllerName' => $controllerName,'moduleName' => $moduleName, 'description' => $description));

    $creationOK = unserialize($data);

	if('OK' == $creationOK['status']) $this->view->messageDisplay()->setMessage('Action has been successfully created','confirmation');
	else $this->view->messageDisplay()->setMessage($creationOK['error']['message'],'error');

    $this->_forward('controller');
  } //end createnewaction









  function actionAction() {
  	$actionName = $this->_getParam('actionName');
  	$controllerName = $this->_getParam('controllerName');
  	$moduleName = $this->_getParam('moduleName');
	
  	if(empty($actionName) ||empty($controllerName) || empty($moduleName)) {
  		$this->_forward('module');
  		return;	
  	}
  	
    $engine = Myzen_Oauth_Query_Singleton::getInstance();    
    $data = $engine->query('apidefinition','action','get',array('name' => $actionName,'controllerName' => $controllerName,'moduleName' => $moduleName));

    $actionData = unserialize($data);

    if('KO' == $actionData['status']) {
      $this->view->messageDisplay()->setMessage($actionData['error']['message'],'error');
   	  $this->_forward('controller');
  	  return;	   	
    }

Zend_Registry::get('app_log')->log($actionData,7);

    $this->view->actionName = $actionData['action']['name'];
    $this->view->controllerName = $actionData['action']['controllerName'];
    $this->view->moduleName = $actionData['action']['moduleName'];
    
    $this->view->description = $actionData['action']['description'];

    $this->getHelper('viewRenderer')->setNoRender(false);
    
    if('KO' == $actionData['message']['status']) {
      $this->view->inputDefinition = array();
    } else {
      $this->view->inputDefinition = $actionData['message']['message']['inputDefinition'];
      if(!empty($actionData['message']['message']['errorsList'])) $this->view->errorsList = $actionData['message']['message']['errorsList'];
      if(!empty($actionData['message']['message']['examplesDefinition'])) {
	      $examples = $actionData['message']['message']['examplesDefinition'];
	      //we generate the examples in serializedPHP and JSON
	      foreach($examples as $num => $example) {
			
			$outputJSON = @json_encode($example['outputValues']);
			if(!empty($outputJSON)) {
	   			$data = $engine->query('apidefinition','message','formatexamplefromjson',array(
      			'JSONString' => $outputJSON,
      			'outputFormat' => 'serializedphp'
      		));
    		$res = unserialize($data);
    	  	$outputSerilializedPHP = '';
    	  	if($res['status'] == 'OK') $outputSerilializedPHP = $res['formattedString'];
    	
	      	$examples[$num]['outputSerializedPHP'] = $outputSerilializedPHP;
	      	$examples[$num]['outputJSON'] = $outputJSON;
	      } //end output JSON not empty
	      } //end for each example
	      $this->view->examples = $examples;	
      
    } //end examples not empty 
    
    //Need to load some YUI javascript to perform the asynchronous call to test API
    $this->view->headScript()->appendFile($this->view->baseUrl().'/ressources/apidesigner/javascript/pages/action.js', 'text/javascript');
    $this->view->YUILoader()->load(array('yahoo', 'dom', 'event','connection','json'));
    
    
    }//End message retrieval OK



	$this->view->Breadcrumbs(array(
		0 => array(
			'text' => 'API Designer', 
			'url'  => '/apidesigner/'
			),
		1 => array(
			'text' => $actionData['action']['moduleName'], 
			'url'  => '/apidesigner/'.$actionData['action']['moduleName'].'/'
			),
		2 => array(
			'text' => $actionData['action']['controllerName'], 
			'url'  => '/apidesigner/'.$actionData['action']['moduleName'].'/'.$actionData['action']['controllerName'].'/'
			),
		3 => array(
			'text' => $actionData['action']['name']
			)
		)		
	);
	$this->saveSidebarLinks();



    
  } //end actionAction
  
  function editactionAction() {

  	$actionName = $this->_getParam('actionName');
  	$controllerName = $this->_getParam('controllerName');
  	$moduleName = $this->_getParam('moduleName');
  	$id = $this->_getParam('id');
	
  	if(empty($actionName) ||empty($controllerName) || empty($moduleName)) {
  		$this->_forward('module');
  		return;	
  	}
  	
    $engine = Myzen_Oauth_Query_Singleton::getInstance();    
    $data = $engine->query('apidefinition','message','get',array('actionName' => $actionName,'controllerName' => $controllerName,'moduleName' => $moduleName,'id' => $id));

    $messageData = unserialize($data);
 
 	if('KO' == $messageData['status']) {
 	  $this->view->messageDisplay()->setMessage($messageData['error']['message'],'error');
 	  $this->_forward('action');
 	  return; 
 	}  

    $this->view->actionName = $actionName;
    $this->view->controllerName = $controllerName;
    $this->view->moduleName = $moduleName;
 
 	if(!empty($id)) $this->view->id = $id;
    
    $this->view->messageDefinition = json_encode($messageData['message']);
    $this->view->action = 'edit';
    
  	
    $this->getHelper('viewRenderer')->setNoRender(false);

	//Load javascript files to perform the magic	
	$this->view->headScript()->appendFile($this->view->baseUrl().'/ressources/apidesigner/javascript/tableConfig.js', 'text/javascript');    
	$this->view->headScript()->appendFile($this->view->baseUrl().'/ressources/apidesigner/javascript/tableTree.js', 'text/javascript');
	$this->view->headScript()->appendFile($this->view->baseUrl().'/ressources/apidesigner/javascript/tableTreeDD.js', 'text/javascript');
	$this->view->headScript()->appendFile($this->view->baseUrl().'/ressources/apidesigner/javascript/exampleEditor.js', 'text/javascript');	
	$this->view->headScript()->appendFile($this->view->baseUrl().'/ressources/apidesigner/javascript/pages/editAction.js', 'text/javascript');
  	$this->view->YUILoader()->load(array('yahoo', 'dom', 'event','treeview','connection','json','animation','dragdrop','container','datasource','autocomplete','datatable'));




	$this->view->Breadcrumbs(array(
		0 => array(
			'text' => 'API Designer', 
			'url'  => '/apidesigner/'
			),
		1 => array(
			'text' => $moduleName, 
			'url'  => '/apidesigner/'.$moduleName.'/'
			),
		2 => array(
			'text' => $controllerName, 
			'url'  => '/apidesigner/'.$moduleName.'/'.$controllerName.'/'
			),
		3 => array(
			'text' => $actionName, 
			'url'  => '/apidesigner/'.$moduleName.'/'.$controllerName.'/'.$actionName.'/'
			),
		4 => array(
			'text' => 'edit'
			)
		)		
	);
	$this->saveSidebarLinks();


  }












  function savemessageAction() {
  	
  	$actionName = $this->_getParam('actionName');
  	$controllerName = $this->_getParam('controllerName');
  	$moduleName = $this->_getParam('moduleName');
    $messageDefinition = $this->_getParam('messageDefinition');
    
    if(empty($messageDefinition) || empty($actionName) || empty($controllerName) || empty($moduleName)) {
      $this->view->messageDisplay()->setMessage('Mandatory information is missing','error');
 	  $this->_forward('editaction');
 	  return;	
    }
    $dataMessage = @json_decode($messageDefinition,true);
    
    if(empty($dataMessage)) {
      $this->view->messageDisplay()->setMessage('Message definition is badly formatted','error');
 	  $this->_forward('editaction');
 	  return;	    	
    }
    $serializedMessage = serialize($dataMessage);
    
    $engine = Myzen_Oauth_Query_Singleton::getInstance();    
    $data = $engine->query('apidefinition','message','create',array(
      'actionName' => $actionName,
      'controllerName' => $controllerName,
      'moduleName' => $moduleName,
      'message' => $serializedMessage,
      'messageFormat' => 'serializedPHP',
      'description' => $this->_getParam('messageChangeDescription')
      ));
    $updateOK = unserialize($data);
    
   	if('KO' == $updateOK['status']) $this->view->messageDisplay()->setMessage($updateOK['error']['message'],'error');
    else  $this->view->messageDisplay()->setMessage('message has been saved','confirmation');
 	$this->_forward('action');
    

  }
  
  function searcherrorAction() {
    $engine = Myzen_Oauth_Query_Singleton::getInstance(); 
    $data = $engine->query('apidefinition','error','search',array(
      'searchString' => $this->_getParam('searchString'),
      ));
      $res = unserialize($data);
      $this->_helper->layout->disableLayout();
      $this->getHelper('viewRenderer')->setNoRender();
      echo json_encode($res); 	
      return;
  }
  
  
  
  
  
  
  
  
  
  
    function actionhistoryAction() {
  	$actionName = $this->_getParam('actionName');
  	$controllerName = $this->_getParam('controllerName');
  	$moduleName = $this->_getParam('moduleName');
	
  	if(empty($actionName) ||empty($controllerName) || empty($moduleName)) {
  		$this->_forward('module');
  		return;	
  	}
  	
    $engine = Myzen_Oauth_Query_Singleton::getInstance();    
    $data = $engine->query('apidefinition','action','gethistory',array('name' => $actionName,'controllerName' => $controllerName,'moduleName' => $moduleName));
    $actionHistoryData = unserialize($data);

    if('KO' == $actionHistoryData['status']) {
      $this->view->messageDisplay()->setMessage($actionHistoryData['error']['message'],'error');
   	  $this->_forward('controller');
  	  return;	   	
    }

Zend_Registry::get('app_log')->log($actionHistoryData,7);

    $actionName = $actionHistoryData['action']['name'];
    $controllerName = $actionHistoryData['action']['controllerName'];
    $moduleName = $actionHistoryData['action']['moduleName'];
    
    $this->view->description = $actionHistoryData['action']['description'];

	$this->view->actionHistory = $actionHistoryData['history'];
	
	//used for the breadcrumb
	$this->view->action = 'history';
	
    $this->getHelper('viewRenderer')->setNoRender(false);
    

	$this->view->Breadcrumbs(array(
		0 => array(
			'text' => 'API Designer', 
			'url'  => '/apidesigner/'
			),
		1 => array(
			'text' => $moduleName, 
			'url'  => '/apidesigner/'.$moduleName.'/'
			),
		2 => array(
			'text' => $controllerName, 
			'url'  => '/apidesigner/'.$moduleName.'/'.$controllerName.'/'
			),
		3 => array(
			'text' => $actionName, 
			'url'  => '/apidesigner/'.$moduleName.'/'.$controllerName.'/'.$actionName.'/'
			),
		4 => array(
			'text' => 'history'
			)
		)		
	);
	$this->saveSidebarLinks();
    
    
  } //end actionhistoryAction
  
  
  
  
  
  
  
  function makemessagecurrentAction() {
  	$actionName = $this->_getParam('actionName');
  	$controllerName = $this->_getParam('controllerName');
  	$moduleName = $this->_getParam('moduleName');
	$id =  $this->_getParam('id');
  	if(empty($actionName) ||empty($controllerName) || empty($moduleName) || empty($id)) {
  		$this->_forward('action');
  		return;	
  	}
  	
    $engine = Myzen_Oauth_Query_Singleton::getInstance();    
    $data = $engine->query('apidefinition','message','makecurrent',array('actionName' => $actionName,'controllerName' => $controllerName,'moduleName' => $moduleName,'id' => $id));
    $makeCurrentResultData = unserialize($data);  	
 
    if('KO' == $makeCurrentResultData['status']) {
      $this->view->messageDisplay()->setMessage($makeCurrentResultData['error']['message'],'error');
    } else {
      $this->view->messageDisplay()->setMessage('Message is now the current version','confirmation');    		
    }

   	$this->_forward('action');
  	return;
  	
  }
  

  function deprecatemessageAction() {
  	$actionName = $this->_getParam('actionName');
  	$controllerName = $this->_getParam('controllerName');
  	$moduleName = $this->_getParam('moduleName');
	$id =  $this->_getParam('id');
  	if(empty($actionName) ||empty($controllerName) || empty($moduleName) || empty($id)) {
  		$this->_forward('action');
  		return;	
  	}
  	
    $engine = Myzen_Oauth_Query_Singleton::getInstance();    
    $data = $engine->query('apidefinition','message','deprecate',array('actionName' => $actionName,'controllerName' => $controllerName,'moduleName' => $moduleName,'id' => $id));
    $deprecateResultData = unserialize($data);  	
 
    if('KO' == $deprecateResultData['status']) {
      $this->view->messageDisplay()->setMessage($deprecateResultData['error']['message'],'error');
    } else {
      $this->view->messageDisplay()->setMessage('Message has been deprecated','confirmation');    		
    }

   	$this->_forward('action');
  	return;
  	
  }
  
  function createerrorAction() {
    $engine = Myzen_Oauth_Query_Singleton::getInstance(); 
    $data = $engine->query('apidefinition','error','create',array(
      'text' => $this->_getParam('errorString'),
      ));
      $res = unserialize($data);
      $this->_helper->layout->disableLayout();
      $this->getHelper('viewRenderer')->setNoRender();
      echo json_encode($res); 	
      return;  	
  }
  
  function formatexampleAction() {
  	
  	$JSONString = $this->_getParam('JSONString');
  	$outputFormat = $this->_getParam('outputFormat');
  	
    $engine = Myzen_Oauth_Query_Singleton::getInstance(); 
    $data = $engine->query('apidefinition','message','formatexamplefromjson',array(
      'JSONString' => $JSONString,
      'outputFormat' => $outputFormat
      ));
      $res = unserialize($data);
      $this->_helper->layout->disableLayout();
      $this->getHelper('viewRenderer')->setNoRender();
      echo json_encode($res); 	
      return;  
      
      	
  }
  
  function testapiAction() {

    $this->_helper->layout->disableLayout();
    $this->getHelper('viewRenderer')->setNoRender();

	$actionName = $this->_getParam('actionName');
	$controllerName = $this->_getParam('controllerName');
  	$moduleName = $this->_getParam('moduleName'); 		
  
  	if(empty($actionName) ||empty($controllerName) || empty($moduleName)) {
  		return 'missing pieces of data';
  	}
  	
  	$params = $this->_getAllParams();
  	//We get the params from the call
  	$callParams = array();
  	if(!empty($params)) {
	  	foreach($params as $name => $value) {
	  		if( 'param_'  == substr  ($name,0,6)) {
	  			$callParams[substr  ($name,6)] = $value;  
	  		}
	  	}
  	}
  	
  	$engine = Myzen_Oauth_Query_Singleton::getInstance();    
    $data = $engine->query($moduleName,$controllerName,$actionName,$callParams);



    
	$param_output = $this->_getParam('param_output');
	if('simplexml' == $param_output) {
		header("content-type: application/xml");
	}	
	 
    echo $data;
  	
  	
  
  }
  
  
  function saveSidebarLinks() {
  	$links = array(
		0 => array(
			'text' => 'Modules List', 
			'url'  => '/apidesigner/'
			)
		);
	
	if($this->_getParam('controller') == 'index' && $this->_getParam('action') =='index' ) $links[0]['selected'] = 1;
	
		
	$this->view->sidebarLinks($links);
	
	
		
  }
  

} //End class

?>